﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace BDM
{
    public partial class AutoForm : Form
    {
        #region Variables
        Database db = new Database();
        Dictionary<int, string> dPorts = new Dictionary<int, string>();
        Dictionary<int, string> dClasses = new Dictionary<int, string>();
        BDMForm formBDM;
        Message msg = new Message();
        #endregion

        public AutoForm(BDMForm frmBDM)
        {
            InitializeComponent();
            startUp();
            formBDM = frmBDM;
        }

        public void startUp()
        {
            loadPorts();
            loadClasses();
        }

        public void loadPorts()
        {
            DataTable dt = GetData.GetAllPortsAsDataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dPorts.Add((int)dt.Rows[i][0], dt.Rows[i][1].ToString());
                cbPoortje.Items.Add(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString());
            }

            if (cbPoortje.Items.Count > 0)
            {
                cbPoortje.SelectedIndex = 0;
            }
        }

        public void loadClasses()
        {
            DataTable dt = GetData.GetAllClassesAsDataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dClasses.Add((int)dt.Rows[i][0], dt.Rows[i][1].ToString());
                cbKlasse.Items.Add(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString());
            }

            if (cbKlasse.Items.Count > 0)
            {
                cbKlasse.SelectedIndex = 0;
            }
        }

        private void tbID_TextChanged(object sender, EventArgs e)
        {
            tbVorige.Text = "";
            cbKlasse.Enabled = true;
        }

        private void btnRijden_Click(object sender, EventArgs e)
        {
            checkDigits();
            if (tbID.Text.ToString() != "" && cbPoortje.SelectedItem.ToString() != "" && cbKlasse.SelectedItem.ToString() != "")
            {
                if (tbVorige.Text == "")
                {
                    cbKlasse.Enabled = false;
                    if ((cbPoortje.SelectedIndex + 1).ToString().Length < 2)
                    {
                        tbVorige.Tag = "0" + (cbPoortje.SelectedIndex + 1).ToString();
                    }
                    else
                    {
                        tbVorige.Tag = (cbPoortje.SelectedIndex + 1).ToString();
                    }
                    tbVorige.Text = cbPoortje.SelectedItem.ToString();
                    string str = tbVorige.Tag.ToString() + "a" + tbID.Text + (cbKlasse.SelectedIndex + 1) + "00";
                    msg.Send(str, tbVorige.Tag.ToString());
                }
                else
                {
                    string sPoort = "00";
                    if ((cbPoortje.SelectedIndex + 1).ToString().Length < 2)
                    {
                        sPoort = "0" + (cbPoortje.SelectedIndex + 1).ToString();
                    }
                    else
                    {
                        sPoort = (cbPoortje.SelectedIndex + 1).ToString();
                    }
                    string str = sPoort + "a" + tbID.Text + (cbKlasse.SelectedIndex + 1) + tbVorige.Tag.ToString();
                    if (tbVorige.Tag.ToString() == sPoort)
                    {
                        MessageBox.Show("Het is niet mogelijk om twee keer langs dezelfde tolpoort te rijden!");
                        return;
                    }
                    msg.Send(str, sPoort);

                    if ((cbPoortje.SelectedIndex + 1).ToString().Length < 2)
                    {
                        tbVorige.Tag = "0" + (cbPoortje.SelectedIndex + 1).ToString();
                    }
                    else
                    {
                        tbVorige.Tag = (cbPoortje.SelectedIndex + 1).ToString();
                    }
                    tbVorige.Text = cbPoortje.SelectedItem.ToString();
                }
            }
            else
            {
                MessageBox.Show("Gelieve alle velden in te vullen!");
            }
        }

        private void checkDigits()
        {
            if (tbID.Text.Length > 0)
            {
                tbID.Text = tbID.Text.PadLeft(4, '0');
            }
        }

        private void tbID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() != "\b" && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void tbID_Leave(object sender, EventArgs e)
        {
            checkDigits();
        }

        private void AutoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            formBDM.bAutoOpen = false;
        }
    }
}